//
//  CustomChooseBoxView.m
//  ZhuKuang
//
//  Created by jjkj on 17/7/12.
//  Copyright © 2017年 jjkj. All rights reserved.
//
#define kCellHeight 44

#import "CustomChooseBoxView.h"
#import "CarShopListModel.h"
#import "Masonry.h"

@interface CustomChooseBoxView() <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *chooseTableView;//选择菜单

@property (nonatomic,strong) UIView *clearView;



@end


@implementation CustomChooseBoxView

-(void)awakeFromNib {
    [super awakeFromNib];
    
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;
    self.layer.borderColor = [UIColor colorWithHexString:@"#1395CE"].CGColor;
    self.layer.borderWidth = 1;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return  self;
}
- (void)setUp {
    [[NSBundle mainBundle] loadNibNamed:@"CustomChooseBoxView" owner:self options:nil];
    
    [self addSubview:self.view];
    
    self.view.frame = self.bounds;

}

-(UITableView *)chooseTableView {
    if (!_chooseTableView) {
        _chooseTableView = [[UITableView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.frame), CGRectGetMaxY(self.frame) , CGRectGetWidth(self.frame), kCellHeight * self.dataArray.count) style:(UITableViewStylePlain)];
        _chooseTableView.delegate = self;
        _chooseTableView.dataSource = self;
        _chooseTableView.layer.borderColor = [UIColor colorWithHexString:@"#1A7DC4"].CGColor;
        _chooseTableView.layer.borderWidth = 2;
        _chooseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }
    
    return _chooseTableView;
}

- (UIView *)clearView {
    
    if (!_clearView) {
        self.clearView = [[UIView alloc] initWithFrame:self.clearViewframe];
        self.clearView.backgroundColor = [UIColor clearColor];
    }
    return _clearView;
}

- (IBAction)chooseBoxClickAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSLog(@"弹出选择框");
    
    [self.window addSubview:self.clearView];
    [self.clearView addSubview:self.chooseTableView];
}


#pragma mark - tableView delegate and datasource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifier = @"resuableCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    cell.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    id model = self.dataArray[indexPath.row];
    
    if ([model isEqual:[NSString class]]) {
        
        cell.textLabel.text = (NSString *)model;
    }else {
        CarShopListModel *carShopModel = (CarShopListModel *)model;
        
        cell.textLabel.text = carShopModel.name;
    }
    
    cell.textColor = [UIColor colorWithHexString:@"#13A2DB"];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    id chooseResult = self.dataArray[indexPath.row];
    
    if (self.chooseBoxClickBlock) {
        self.chooseBoxClickBlock(chooseResult);
    }
    
    NSString *title = @"";
    
    if ([chooseResult isEqual:[NSString class]]) {
        
         title = (NSString *)chooseResult;
    }else {
        CarShopListModel *carShopModel = (CarShopListModel *)chooseResult;
        
        title = carShopModel.name;
    }

    
    [self changeChooseBoxTitle:title];
    
    [self.clearView removeFromSuperview];
    [self.chooseTableView removeFromSuperview];
}

- (void)changeChooseBoxTitle:(NSString *)chooseResult {
    
    [self.chooseButton setTitleColor:[UIColor colorWithHexString:@"#13A2DB"] forState:(UIControlStateNormal)];
    [self.chooseButton setTitleColor:[UIColor colorWithHexString:@"#13A2DB"] forState:(UIControlStateHighlighted)];
    [self.chooseButton setTitleColor:[UIColor colorWithHexString:@"#13A2DB"] forState:(UIControlStateSelected)];
    
    NSMutableAttributedString *titleStr = (NSMutableAttributedString *)[CommClass getTitleStr:chooseResult withImgName:@"blue_arow" withImgY:0];
  
    [titleStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#13A2DB"] range:NSMakeRange(0, titleStr.length)];
    
    [self.chooseButton setAttributedTitle:titleStr forState:(UIControlStateNormal)];
    [self.chooseButton setAttributedTitle:titleStr forState:(UIControlStateHighlighted)];
    [self.chooseButton setAttributedTitle:titleStr forState:(UIControlStateSelected)];
    
    
}
@end
